package com.iptv.grape.jss.impls.listener;

import android.content.SharedPreferences;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.iptv.grape.RunTimeApplication;
import com.iptv.grape.beans.WebBean;

import static android.content.Context.MODE_PRIVATE;
/****
 * 网页交互监听
 */
public class WebCallBack {
    private WebBackImpl webBackImp;
    public WebCallBack(WebBackImpl webBackImp){
        this.webBackImp = webBackImp;
    }
    /**
     * 2.1.6
     *  ACTION_PLAY_HISTORY_FUN
     *  ACTION_PLAY_HISTORY_EDUCATION
     * @param key
     * @param value
     */
    @JavascriptInterface
    public void saveLocalData(String key, String value) {
        SharedPreferences mSharedPreferences = RunTimeApplication.getCurApp().getSharedPreferences("Web_SAVE_DATA", MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mSharedPreferences.edit();
        mEditor.putString(key, value);
        mEditor.commit();
    }

    /***
     * 2.1.6
     * @param key
     * @return
     */
    @JavascriptInterface
    public String getLocalData(String key) {
        SharedPreferences mSharedPreferences = RunTimeApplication.getCurApp().getSharedPreferences("Web_SAVE_DATA", MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mSharedPreferences.edit();
        return mSharedPreferences.getString(key, "");
    }
    public final static String  TAG_WEB_openBST =  "tag_web_openBST";
    public final static String  TAG_WEB_jumpWebUrl = "tag_web_jumpWebUrl";
    public final static String  TAG_WEB_quitActivity = "tag_web_quitActivity";

    public final static String TAG_OEPN_URL_TYPE_WEB = "web";
    public final static String TAG_OEPN_URL_TYPE_OTHER_PAGE = "other_web_page";
    /**
     *  2.1.14  TAG_WEB_openBST
     *  2.3.1   TAG_WEB_jumpWebUrl
     *  2.3.2   TAG_WEB_quitActivity
     *  2.3.3   TAG_WEB_jumpWebUrl
     *   json协议
     */
    @JavascriptInterface
    public void recvWebPlay(String jsonData){
        WebBean bean =new Gson().fromJson(jsonData,WebBean.class);
        if(bean!=null&&webBackImp!=null)
            webBackImp.recvWebImpl(bean);

    }



    public interface WebBackImpl{
         void recvWebImpl(WebBean bean);
    }

}
